﻿using System;
using System.IO;

namespace ProjectEuler.Problems
{
    public abstract class EulerProblem
    {
        public abstract Object Solve();

        protected TextReader OpenTextAttachment()
        {
            return new StreamReader(OpenAttachment(".txt"));
        }

        protected Byte[] ReadBinaryAttachment()
        {
            using (var stream = OpenAttachment(".bin"))
            {
                using (var ms = new MemoryStream())
                {
                    var read = 0;
                    var buffer = new Byte[65536];

                    do
                    {
                        read = stream.Read(buffer, 0, buffer.Length);

                        if (read > 0)
                        {
                            ms.Write(buffer, 0, read);
                        }
                    }
                    while (read > 0);

                    return ms.ToArray();
                }
            }
        }

        protected Stream OpenAttachment(String suffix)
        {
            var t = GetType();
            var name = t.Name + suffix;

            return t.Assembly.GetManifestResourceStream(t, name);
        }
    }
}
